package com.yingxue.lesson.service.impl;

import com.yingxue.lesson.dao.SysUserRoleDao;
import com.yingxue.lesson.entity.SysUserRole;
import com.yingxue.lesson.exception.BaseResponseCode;
import com.yingxue.lesson.exception.BusinessException;
import com.yingxue.lesson.service.SysUserRoleService;
import com.yingxue.lesson.vo.req.UserOwnRoleReqVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * (SysUserRole)表服务实现类
 *
 * @author makejava
 * @since 2022-04-05 21:41:16
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Resource
    private SysUserRoleDao sysUserRoleDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysUserRole queryById(String id) {
        return this.sysUserRoleDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sysUserRole 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SysUserRole> queryByPage(SysUserRole sysUserRole, PageRequest pageRequest) {
        long total = this.sysUserRoleDao.count(sysUserRole);
        return new PageImpl<>(this.sysUserRoleDao.queryAllByLimit(sysUserRole, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sysUserRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserRole insert(SysUserRole sysUserRole) {
        this.sysUserRoleDao.insert(sysUserRole);
        return sysUserRole;
    }

    /**
     * 修改数据
     *
     * @param sysUserRole 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserRole update(SysUserRole sysUserRole) {
        this.sysUserRoleDao.update(sysUserRole);
        return this.queryById(sysUserRole.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.sysUserRoleDao.deleteById(id) > 0;
    }

    @Override
    public List<String> getRoleIdsByUserId(String userId) {
        return sysUserRoleDao.getRoleIdsByUserId(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUserRoleInfo(UserOwnRoleReqVO vo) {
        //删除他们关联数据
        sysUserRoleDao.removeRoleByUserId(vo.getUserId());
        if(vo.getRoleIds()==null||vo.getRoleIds().isEmpty()){
            return;
        }
        List<SysUserRole> list=new ArrayList<>();
        for (String roleId:
                vo.getRoleIds()) {
            SysUserRole sysUserRole=new SysUserRole();
            sysUserRole.setId(UUID.randomUUID().toString());
            sysUserRole.setCreateTime(new Date());
            sysUserRole.setUserId(vo.getUserId());
            sysUserRole.setRoleId(roleId);
            list.add(sysUserRole);
        }
        int i = sysUserRoleDao.batchInsertUserRole(list);
        if(i==0){
            throw new BusinessException(BaseResponseCode.OPERATION_ERROR);
        }
    }
    @Override
    public List<String> getUserIdsByRoleIds(List<String> roleIds) {
        return sysUserRoleDao.getUserIdsByRoleIds(roleIds);
    }
}
